package com.example.demo.sqlInjector;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.joining;

public class MyGet extends AbstractMethod {

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        List<String> whereSql = new ArrayList<>();
        List<String> selectSql = new ArrayList<>();
        List<String> joinSql = new ArrayList<>();
        MySqlUtil.getAllSqlFromClass(modelClass, whereSql,selectSql, joinSql,false);
        String where = "where "+ StringUtils.firstToLowerCase(modelClass.getSimpleName())+".id = #{id}";
        String sql = "<script>select %s from %s %s  %s</script>";
        sql = String.format(sql,selectSql.stream().collect(joining(",")),tableInfo.getTableName(),joinSql.stream().collect(joining(" ")), where);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForTable(mapperClass, "myGet", sqlSource, tableInfo);
    }
}
